% vivado project QPSK_IMPLEMENTATION_USING_AD9361
clc;
clear;
close all;

% ------------------------------Transmitter-----------------------------------------
%% Params Init
Fs = 10e6; %sample frequency
Fd = 1e3;
Bd = 2.5e6; %baud rate
M = 4; %QPSK
OSR = Fs / Bd; %samples per symbol
EBN0 = 11;
SNR = EBN0 + 10 * log10(2 / OSR);
ROLLOF = 0.5; %roll-off factor for rcosdesign func

%% data generate
data = 1 * exp(sqrt(-1) * 2 * pi * randi([0 M - 1], 1, 50000) / M);

%% upsample, shaping filter, farrow interpolation
% 4x upsample and shaping filter using upfirdn
rcos_fir = round(1.15 * rcosdesign(ROLLOF, 6, Fs / Bd, 'sqrt') * 2^15);
% 12+0 * 1+15 -> 1+11
data_rcos = upfirdn(data, rcos_fir, OSR) / 16;
% farrow interpolation using farrow func
data_tx = floor(farrow_int(Fs, 60e6, data_rcos));
clear data_rcos

%% modulation and channel transmission
signal_carrier = exp(sqrt(-1) * 2 * pi * (0:length(data_tx) - 1) / 60e6 * Fd)';
tx_signal = signal_carrier .* data_tx;
signal_rx = awgn(tx_signal, SNR, 'measured');
signal_rx = floor(signal_rx / max(abs(signal_rx)) * 2^11);

%% ------------------------------Receiver-----------------------------------------
%% demodulation, downsample and matched filter
% signal_rx = signal_rx .* exp(-1 * sqrt(-1) * 2 * pi * (0:length(signal_rx) - 1) / 60e6 * Fd)';
signal_rx(1:10000) = 0.3*signal_rx(1:10000);
signal_rx(10001:30000) = 0.8*signal_rx(10001:30000);
save_data_to_txt_file(signal_rx,1200000,'pll_5');